package liuyuyang.controller;

import liuyuyang.domain.Permission;
import liuyuyang.pojo.Result;
import liuyuyang.service.PermissionService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/perm")
public class PermissionController {
    @Resource
    private PermissionService permissionService;

    @PostMapping
    public Result<String> add(@RequestBody Permission permission) {
        try {
            permissionService.add(permission);
            return Result.success("添加成功");
        } catch (Exception e) {
            return Result.error("添加失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/{pid}")
    public Result<String> del(@PathVariable Integer pid) {
        try {
            permissionService.del(pid);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error("删除失败" + e.getMessage());
        }
    }

    @PatchMapping
    public Result<String> edit(@RequestBody Permission permission) {
        try {
            permissionService.edit(permission);
            return Result.success("修改成功");
        } catch (Exception e) {
            return Result.error("修改失败：" + e.getMessage());
        }
    }

    @GetMapping("/role/{rid}")
    public Result<List<Permission>> getRolePerm(@PathVariable Integer rid) {
        List<Permission> permission = permissionService.getRolePerm(rid);
        return Result.success(permission);
    }

    @GetMapping
    public Result<List<Permission>> list() {
        List<Permission> list = permissionService.list();
        return Result.success(list);
    }
}
